package com.example.back.impl;

import com.example.back.mapper.OrderMapper;
import com.example.back.model.Contain;
import com.example.back.model.Order;
import com.example.back.model.OrderProduct;
import com.example.back.model.Product;
import com.example.back.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderMapper orderMapper;

    @Override
    public List<Order> findAllOrder() {
        return orderMapper.findAllOrder();
    }

    @Override
    public List<Order> findOrder(String account) {
        return orderMapper.findOrder(account);
    }

    @Override
    public List<OrderProduct> findOrderProduct(int orderID) {
        return orderMapper.findOrderProduct(orderID);
    }

    @Override
    public List<OrderProduct> findOrderProductByType(int orderID, String type) {
        return orderMapper.findOrderProductByType(orderID, type);
    }

    @Override
    public List<OrderProduct> findOrderProductByName(int orderID, String name) {
        return orderMapper.findOrderProductByName(orderID, name);
    }

    @Override
    public OrderProduct findTheProduct(int orderID, int productID) {
        return orderMapper.findTheProduct(orderID, productID);
    }

    @Override
    public int findOrderState(int orderID) {
        return orderMapper.findOrderState(orderID);
    }

    @Override
    public String addOrder(String account, String room) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = df.format(new Date());
        if(orderMapper.addOrder(account, room,  time))
            return "success";
        return "fail";
    }

    @Override
    public int findOrderID(String account, String room) {
        Order order = orderMapper.findOrderID(account, room);
        if(order != null)return order.getOrderID();
        return 0;
    }

    @Override
    public Boolean addProduct(int orderID, int productID, int count, double cost) {
        return orderMapper.addProduct(orderID, productID, count, cost);
    }

    @Override
    public Boolean addCost(int orderID, double cost) {
        return orderMapper.addCost(orderID, cost);
    }

    @Override
    public Boolean findProduct(int orderID, int productID) {
        Contain contain = orderMapper.findProduct(orderID, productID);
        return contain != null;
    }

    @Override
    public Boolean updateProduct(int orderID, int productID, int count, double cost) {
        return orderMapper.updateProduct(orderID, productID, count, cost);
    }

    @Override
    public Boolean removeProduct(int orderID, int productID) {
        return orderMapper.removeProduct(orderID, productID);
    }

    @Override
    public List<Order> findOrderByState(int state) {
        return orderMapper.findOrderByState(state);
    }

    @Override
    public List<Order> findOrderByAccount(String account) {
        return orderMapper.findOrderByAccount(account);
    }

    @Override
    public List<Order> findOrderByRoom(String room) {
        return orderMapper.findOrderByRoom(room);
    }

    @Override
    public Boolean deleteOrder(int orderID) {
        return orderMapper.deleteOrder(orderID);
    }

    @Override
    public Boolean finishOrder(int orderID) {
        return orderMapper.finishOrder(orderID);
    }
}
